package com.yida.auth.service;

import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.yida.common.core.constant.CacheConstants;
import com.yida.common.core.utils.StringUtils;
import com.yida.common.redis.service.RedisService;

/**
 * 
 * 基础数据
 * 
 * @author hmh
 * @date 2022/10/26
 */
@Component
public class BasicDataService {

    @Autowired
    public RedisService redisService;

    // 短信验证码校验开关
    private final String isPhoneCode = "sys.phonenumber.codeEnabled";

    // 系统别名
    private final String sysAlias = "sys.alias.nameEnabled";

    /**
     * 获取基础数据
     * 
     * @return 结果
     */
    public Map<String, Object> basicConfig() {
        Map<String, Object> map = new HashMap<String, Object>();
        // 获取手机校验开关
        String codeEnabled = redisService.getCacheObject(getCacheKey(isPhoneCode));
        if (StringUtils.isEmpty(codeEnabled)) {
            codeEnabled = "true";
        }
        // 系统别名
        String sysAliasName = redisService.getCacheObject(getCacheKey(sysAlias));
        if (StringUtils.isEmpty(sysAlias)) {
            sysAliasName = "住宅专项维修资金管理平台";
        }
        map.put("codeEnabled", codeEnabled);
        map.put("sysAliasName", sysAliasName);
        // 获取短信等待时间
        Long semExpirationTime = NumberUtils.toLong(redisService.getCacheObject(getCacheKey(CacheConstants.SMS_CODE_EXPIRATION_TIME_KEY)));
        map.put("sendSmsWait", semExpirationTime > 0 ? semExpirationTime : CacheConstants.SMS_CODE_EXPIRATION);
        return map;
    }

    /**
     * 设置cache key
     * 
     * @param configKey 参数键
     * @return 缓存键key
     */
    public String getCacheKey(String configKey) {
        return CacheConstants.SYS_CONFIG_KEY + configKey;
    }

    /**
     * 20221111 hmh 获取手机验证码校验开关
     * 
     * @return 结果
     */
    public String getPhoneCodeEnabled() {
        String codeEnabled = redisService.getCacheObject(getCacheKey(isPhoneCode));
        if (StringUtils.isEmpty(codeEnabled)) {
            codeEnabled = "true";
        }
        return codeEnabled;
    }
}
